package ocp.context.xml;

import ocp.context.*;
import org.xml.sax.*;

/**
 * This class implements a xml reader for the simple context. It reads an xml 
 * file and, if it containts a valid simple context, returns it so the context
 * can update its information.
 *
 * The xml format must be: <br><code>
 * &lt;ContextClass name="name" id="id"&gt;
 * 		&lt;ContextualInformation1Name description="..." type="..."&gt;
 * 			value
 * 		&lt;/ContextualInformation1Name&gt;
 * 		&lt;ContextualInformation2Name description="..." type="..."&gt;
 * 			value
 * 		&lt;/ContextualInformation2Name&gt;
 * 		&lt;ContextualInformation3Name description="..." type="..."&gt;
 * 			value
 * 		&lt;/ContextualInformation3Name&gt;
 *
 *		...
 * 		
 * 		&lt;ContextualInformationNName description="..." type="..."&gt;
 * 			value
 * 		&lt;/ContextualInformationNName&gt;
 * &lt;/ContextClass&gt;
 * </code><br>
 *
 * @author Ignacio Nieto Carvajal.
 */
public class XMLSimpleContextReader extends XMLContextReader
{
	protected ContextualInformationList data = new ContextualInformationList();
	protected boolean elementStarted;
	
	public XMLSimpleContextReader(String xmlFile) { super(xmlFile); }
	
	public XMLSimpleContextReader(String xmlFile, String parserName) {
		super(xmlFile, parserName);
	}

	public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException
	{
		if (level == 1)
		{
			try {
				Class c = Class.forName(localName);
				if (!c.isInstance(Class.forName("SimpleContext"))) {
					System.err.println(
						"Class "+localName+" is not a valid Simple Context");
					elementStarted = false;
					return;
				}
			} catch (ClassNotFoundException cnfe)	{
				System.err.println("Class "+localName+" not found");
				elementStarted = false;
				return;
			}
			elementStarted = true;
		}
		else
		{
			elementStarted = true;
		}

		level++;
	}

	public void endElement(String uri, String localName, String qName) 
        throws SAXException
	{
		if (elementStarted) elementStarted = false;
		else return;

		// tratamiento del elemento.
		
		level--;
	}

	public Object readContext() throws IncompatibleContextualObjectException {
		return null;
	}
}
